package com.xcc.mrpbmp;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.image)
    ImageView imageView;
    @BindView(R.id.textView)
    TextView textView;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.menuBtn) {
            startActivity(new Intent(this, JpgActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.image)
    public void onImageClicked() {
        int length = BmpLoad.W.length;
        loadBitmap(index);
        index++;
        if (index >= length) index = 0;
    }

    private void loadBitmap(final int index) {
        final BmpLoad bmpLoad = new BmpLoad(this);
        new Thread() {
            @Override
            public void run() {
                long timeMillis = System.currentTimeMillis();
                final Bitmap bitmap = bmpLoad.loadBitmap(index);
                final long l = System.currentTimeMillis() - timeMillis;
                if (bitmap == null) return;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageBitmap(bitmap);
                        textView.setText(l + "毫秒");
                    }
                });
            }
        }.start();
    }
}
